﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using DriveBabyDrive.Models;
using System.Web.Security;
using DataLayer;
using System.Data;

namespace DriveBabyDrive.Controllers
{
    public class ProfileController : Controller
    {
        //
        // GET: /Profile/
        [HttpGet]
        public ActionResult Index()
        {

            //get the user and prep the model
            UserProfileModel model= getUserProfileModelForUser( Membership.GetUser().UserName);
            
            //get States list
            ViewData["StatesList"] = getPossibleStatesList();
            string browserAgent = Request.UserAgent.ToString().ToLower();
            if (!(browserAgent.Contains("android") || browserAgent.Contains("iphone")))
            {
                return RedirectToAction("IndexDesktop");
            } 
            return View(model);
        }
        //
        // GET: /Profile/
        [HttpPost]
        public ActionResult Index( UserProfileModel model)
        {
            string userName=Membership.GetUser().UserName;
            //save the model back to the DB
            saveUserProfile(model, userName);
            model = getUserProfileModelForUser(userName);
            //get States list
            ViewData["StatesList"] = getPossibleStatesList();
            ViewBag.SaveMessage = "Changes Saved sucessfully";
            int vehicleCount = (new VehiclesDL()).getUserVehicles(userName).Tables[0].Rows.Count;
            if (vehicleCount == 0)
            {
                return RedirectToAction("MyVehicles", "Profile");
            }
            else
            {
                return View(model);
            }
            
        }


        [HttpGet]
        public ActionResult MyVehicles()
        {
            //get the user vehicles 
            DataSet dsUserVehicles = (new VehiclesDL()).getUserVehicles(Membership.GetUser().UserName);
            //get vehicles for the user
            ViewData["UserVehicles"] = dsUserVehicles.Tables[0].Rows;
            ViewData["StatesList"] = getPossibleStatesList();
            return View();
        }
        [HttpGet]
        public ActionResult IndexDesktop()
        {
            ViewData["StatesList"] = getPossibleStatesList();            
            return View(this.getUserProfileModelForUser(Membership.GetUser().UserName));
        }
        [HttpPost]
        public ActionResult IndexDesktop(UserProfileModel model)
        {
            //save to DB
            this.saveUserProfile(model, Membership.GetUser().UserName);
            ViewData["StatesList"] = getPossibleStatesList(); 
            return View();
        }
        /// <summary>
        /// Gets a list of vehicles to pick from
        /// </summary>
        /// <returns></returns>
        public ActionResult VehicleList()
        {
            DataSet dsVehicles = (new VehiclesDL()).getVehicles();
            ViewData["VehicleList"] = dsVehicles.Tables[0].Rows;
            return View();
        }

        /// <summary>
        /// Adds vehicle
        /// </summary>
        /// <param name="vehicleId"> id of the vehicle to add</param>
        /// <returns></returns>
        public ActionResult AddVehicle(int vehicleId)
        {
            if (Membership.GetUser() != null)
            {
                //code to add vehicle to the users
                (new VehiclesDL()).addUserVehicles(Membership.GetUser().UserName, vehicleId);
            }

            return RedirectToAction("UpdateVehicle", new { vehicleId = vehicleId }); 
        }
        [HttpGet]
        public ActionResult UpdateVehicle(int vehicleId)
        {
            //get vehicle to update
            VehicleModel vehicle=this.getVehicleModelByVehicleID(vehicleId);
            return View(vehicle);
        }
        /// <summary>
        /// Updates the Vehicle added by the user
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateVehicle(VehicleModel model)
        {
            (new VehiclesDL()).updateUserVehicleName(model.VehicleID, model.VehicleName);
            return RedirectToAction("MyVehicles"); 
        }
        /// <summary>
        /// Deletes a User Vehicle
        /// </summary>
        /// <param name="vehicleId"></param>
        /// <returns></returns>        
        public ActionResult DeleteVehicle(int vehicleId)
        {
            (new VehiclesDL()).deleteUserVehicle(Membership.GetUser().UserName, vehicleId);
            return RedirectToAction("MyVehicles"); 
        }

        #region Model saves and gets
        /// <summary>
        /// Saves user profile info
        /// </summary>
        /// <param name="model"></param>
        /// <param name="username"></param>
        private void saveUserProfile(UserProfileModel model, string username)
        {
           (new UsersDL()).saveAdditionalUserInfo(username, model.FirstName, model.LastName, model.Address, model.City, model.State, model.Zip, model.Phone);
        }
        /// <summary>
        ///  generate a list of selected states for pssing to the view
        /// </summary>
        /// <returns></returns>
        private  SelectList getPossibleStatesList()
        {
            var states = new SelectList(new[]
                                          {
                                             new {ID="IL",Name="Illinois"},
                                             new {ID="PA",Name="Pennsylvania"}
                                          },"ID","Name");

            return states;
        }
        /// <summary>
        /// Gets view model for mobile view
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        private UserProfileModel getUserProfileModelForUser(string username)
        {
            UserProfileModel upm = new UserProfileModel();
            DataRow dr = (new UsersDL()).getUserByUsername(username).Tables[0].Rows[0];
            upm.FirstName = dr["FirstName"].ToString();
            upm.LastName = dr["LastName"].ToString();
            upm.Address = dr["Address"].ToString();
            upm.City = dr["City"].ToString();
            upm.State = dr["State"].ToString();
            upm.Zip = dr["Zip"].ToString();
            upm.Phone = dr["Phone"].ToString();
            return upm;
        }
        /// <summary>
        /// gets vehicle model by vehicleid
        /// </summary>
        /// <param name="vehicleId"></param>
        /// <returns></returns>
        private VehicleModel getVehicleModelByVehicleID(int vehicleId)
        {
            VehicleModel vm = new VehicleModel();
            DataRow dr = (new VehiclesDL()).getUserVehicleByVehicleId(Membership.GetUser().UserName, vehicleId).Tables[0].Rows[0];
            vm.VehicleID = int.Parse(dr["VehicleID"].ToString());
            vm.VehicleName = dr["Name"].ToString();
            vm.Make = dr["Make"].ToString();
            vm.Model = dr["Model"].ToString();
            vm.Year = dr["Year"].ToString();
            vm.Trim = dr["Trim"].ToString();
            return vm;
        }
        /// <summary>
        /// Initializes and gets view model for desktop
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        

        #endregion
    }
}
